<template>
	<view class="content">
		<simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" themeColor='#007AFF'></simple-address>
		<!-- <button class="btns" type="primary" @tap="openAddres">默认打开地址</button> -->
		<view class="box">
			<view class="left">
				收件人 ---

			</view>
			<view class="right">
				<input v-model="name" type="text" placeholder="请输入收件人姓名" />
			</view>
		</view>
		<view class="box">
			<view class="left">
				电话号码
			</view>
			<view class="right">
				<input v-model="tel" type="text" placeholder="请输入收件人电话号码" />
			</view>
		</view>
		<view class="box">
			<view class="left">
				所在地区
			</view>
			<view class="right">
				<input @tap="openAddres3" v-model="address" type="text" />
			</view>
		</view>
		<view class="box">
			<view class="left">
				详细地址
			</view>
			<view class="right">
				<input v-model="detailAddress" type="text" placeholder="输入详细地址" />
			</view>
		</view>
		<view class="box">
			<view class="left">
				设置默认地址
			</view>
			<view class="right">
				<!-- <evan-checkbox v-model="checked"></evan-checkbox> -->
				<!-- <switch color="#354e44" v-model="checked" /> -->
				<evan-switch inactive-color="#FDFDFD" active-color="#354e44" v-model="checked"></evan-switch>
			</view>
		</view>
	<!-- 	<view class="del" @tap="del">
			删除
		</view> -->
		<button type="warn" @tap="del">删除收获地址</button>
		<view class="save" @tap="save">
			保存地址
		</view>
		


	</view>
</template>

<script>
	import {
		findAllAddress,
		updateAddressInfo,
		delAddressInfo
	} from "@/api/mine/mine.js"
	export default {
		data() {
			return {
				cityPickerValueDefault: [0, 0, 1],
				cityCode: '',
				defaultStatus: 0,
				checked: false,
				name: "",
				tel: "",
				address: '请点击选择地址',
				detailAddress: '',
				memberId:'',
				region: '',
				province: '',
				city: '',
				id: '',
				list: [],


			}
		},

		onLoad: function(option) {
			this.id = option.id;
			// console.log(this.id)
			this.getdz()


		},



		methods: {
			getdz() {
				findAllAddress().then(res => {
					// console.log(res)
					this.list = res.data.items
					console.log(this.list)
					var info = this.list.filter(el => {
						return el.id == this.id
					})
					console.log(info)
					this.memberId = info[0].memberId
					this.cityCode = info[0].cityCode
					this.name = info[0].name
					this.tel = info[0].phoneNumber
					this.detailAddress = info[0].detailAddress
					this.address = info[0].province + '-' + info[0].city + '-' + info[0].region
					if (info[0].defaultStatus == 1) {
						this.checked = true
					}
				})
			},

			openAddres3() {
				// 根据value 获取
				var arr = this.cityCode;
				if (arr == '') {
					arr = [11, 1101, 110101]
				} else {
					arr = arr.split('-');
					arr.forEach((item, index) => {
						arr[index] = parseInt(arr[index])
					})
				}

				console.log(arr)

				var index = this.$refs.simpleAddress.queryIndex(arr, 'value');
				console.log(index);
				this.cityPickerValueDefault = index.index;
				this.$refs.simpleAddress.open();
			},

			onConfirm(e) {
				console.log(e)
				this.address = e.label
				this.province = e.labelArr[0]
				this.city = e.labelArr[1]
				this.region = e.labelArr[2]
				// console.log(this.region)
				this.cityCode = e.provinceCode + '-' + e.cityCode + '-' + e.areaCode
				console.log(this.cityCode)
			},
			save() {

				if (this.checked) {
					this.defaultStatus = 1
				} else {
					this.defaultStatus = 0
				}
				// console.log(this.defaultStatus)

				updateAddressInfo({
					name: this.name,
					phoneNumber: this.tel,
					defaultStatus: this.defaultStatus,
					detailAddress: this.detailAddress,
					cityCode: this.cityCode,
					city: this.city,
					province: this.province,
					region: this.region,
					id: this.id
				}).then(res => {
					console.log(res)
					if(res.success){
						uni.showToast({
						    title: '操作成功',
						    duration: 2000
						});
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					}else{
						uni.showToast({
						    title: `${res.message}`,
							icon:'none',
						    duration: 2000
						});
					}
				})
			},
			del(){
				console.log(this.id)
				delAddressInfo(this.id).then(res=>{
					console.log(res)
					if(res.success){
						uni.showToast({
						    title: '操作成功',
						    duration: 2000
						});
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					}else{
						uni.showToast({
						    title: `${res.message}`,
							icon:'none',
						    duration: 2000
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		width: 90%;
		box-sizing: border-box;
		height: 150rpx;
		line-height: 50rpx;
		display: flex;
		margin: 0 auto;
		justify-content: space-between;
		align-items: center;
		padding: 40rpx 0rpx;
		border-bottom: 2rpx solid #ccc;

		.left {}

		.input {
			.right {
				margin-right: 50rpx;
			}
		}
		


	}

	.save {
		width: 70%;
		height: 80rpx;
		border-radius: 80rpx;
		background-color: #354e44;
		color: #fff;
		line-height: 80rpx;
		text-align: center;

		margin: 500rpx auto;
	}
	button{
		width: 90%;
		margin-top: 60rpx;
	}
</style>
